package me.zxk.smartagriculture.repository;

import me.zxk.smartagriculture.entity.Like;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 点赞数据访问层
 */
@Repository
public interface LikeRepository extends JpaRepository<Like, Long> {

    /**
     * 根据用户ID、目标ID和目标类型查询点赞记录
     */
    Optional<Like> findByUserIdAndTargetIdAndTargetType(Long userId, Long targetId, Like.TargetType targetType);

    /**
     * 检查用户是否已点赞
     */
    boolean existsByUserIdAndTargetIdAndTargetType(Long userId, Long targetId, Like.TargetType targetType);

    /**
     * 根据目标ID和目标类型统计点赞数
     */
    long countByTargetIdAndTargetType(Long targetId, Like.TargetType targetType);

    /**
     * 根据用户ID查询点赞记录
     */
    List<Like> findByUserId(Long userId);

    /**
     * 根据目标ID和目标类型查询点赞记录
     */
    List<Like> findByTargetIdAndTargetType(Long targetId, Like.TargetType targetType);

    /**
     * 批量查询用户对多个目标的点赞状态
     */
    @Query("SELECT l FROM Like l WHERE l.userId = :userId AND l.targetId IN :targetIds AND l.targetType = :targetType")
    List<Like> findByUserIdAndTargetIdsAndTargetType(@Param("userId") Long userId, 
                                                     @Param("targetIds") List<Long> targetIds, 
                                                     @Param("targetType") Like.TargetType targetType);

    /**
     * 删除用户对目标的点赞
     */
    void deleteByUserIdAndTargetIdAndTargetType(Long userId, Long targetId, Like.TargetType targetType);

    /**
     * 删除用户的所有点赞记录
     */
    void deleteByUserId(Long userId);
}
